package com.greenline.push.home.controllers.message;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guser.annotation.GuserResource;
import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.pushcenter.MsgRetryManager;
import com.greenline.push.dal.pushcenter.dataobject.AppChannelDO;
import com.greenline.push.dal.pushcenter.dataobject.MsgRetryDO;
import com.greenline.push.dal.query.MsgRetryQuery;

@Controller
@RequestMapping(value = "/message")
public class MsgRetryController {

    @Resource
    private MsgRetryManager msgRetryManager;
    private final Log logger = LogFactory.getLog(MsgRetryController.class);

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "msgretry/index";
    }

    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<MsgRetryDO> listRecord(MsgRetryQuery query,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));
        GridJsonData<MsgRetryDO> ret = new GridJsonData<MsgRetryDO>();
        ret.setQuery(msgRetryManager.listByQuery(query));
        ret.setQuery(query);
        return ret;
    }

    @GuserResource("更新消息重发表")
    @RequestMapping(value = "/updatestatus.json", method = RequestMethod.GET)
    public @ResponseBody
    BaseJson delete(ModelMap model) {
        BaseJson json = new BaseJson();
        try {
            msgRetryManager.updateAllSendStatus();
            json.setMessage("更新成功！");
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("删除出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }
}
